package com.dykj.dati.config;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Date;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        if (StringUtils.equals(requestURI, "/timer/t1") ||
                StringUtils.contains(requestURI, "/layui") ||
                StringUtils.contains(requestURI, "/js") ||
                StringUtils.contains(requestURI, "/css")||
        StringUtils.contains(requestURI, "/img")) {
            return true;
        }
        Date start = DateUtil.parse("2024-08-26 09:00:00");
        //Date end= DateUtil.parse("2023-09-01 17:00:00");
        Date end = DateUtil.parse("2024-08-30 17:00:00");

        Date dq = DateUtil.parse("2029-09-01 16:00:00");
        boolean boo = DateUtil.isIn(DateUtil.date(), start, end);
        if (!boo) {
            response.setContentType("text/html;charset=utf-8");
            //1.获取写出流
            /*PrintWriter out = response.getWriter();
            //2.正式写出数据——写到浏览器
            out.write("当前答题未开始！<br>");
            out.write("本次答题开始时间：2023年08月28日 9点00分00秒<br>");
            out.write("本次答题结束时间：2023年09月01日 17点00分00秒<br>");
            out.write("当前时间："+DateUtil.format(DateUtil.date(), "yyyy年MM月dd日 HH时mm分ss秒")+"<br>");
            out.write("敬请期待！<br>");*/
            request.getRequestDispatcher("/timer/t1").forward(request, response);
            return false;
        }
        return true;
    }

    //目标方法执行完成以后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    //页面渲染以后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
